//
//  YXJCDetailVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "GZGJDetailVC.h"
#import "TYBaseTableViewCell.h"
#import "YXJCDetailEChartsView.h"
#import "GZGJDetailCell1.h"
#import "GZGJModel.h"
#import "WXJLModel.h"
#import "MJRefresh/MJRefresh.h"

@interface GZGJDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView * lineImgView;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) TYBoxView * bottomBgView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) YXJCDetailEChartsView *chartView;

@property (nonatomic, assign) NSInteger  currentTag;
@property (nonatomic, strong) UILabel * tipLab;
@property (nonatomic, strong) GZGJModel * model;
@property (nonatomic) NSInteger  page;
@property (nonatomic,strong) UIScrollView * scrollView  ;
@property (nonatomic,strong) UIView * footerView  ;
@property (nonatomic,copy) NSString * devId;

@end

@implementation GZGJDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"告警详情";
    self.dataArr = [NSMutableArray array];
    kWEAK_SELF;
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%@",TYGetAlarmworkorderDetail,self.gjId] parameters:@{@"lx":self.lx} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        if(data){
            weakSelf.model = [GZGJModel yy_modelWithJSON:data[@"data"]];
            weakSelf.devId = weakSelf.model.devId;
            [weakSelf initUI];
        }
        
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];

    
}
- (void)initUI {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, self.view.height)];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.scrollView = scrollView;
    kWEAK_SELF
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf loadRequestList];
    }];
    self.scrollView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf loadRequestList];
    }];
    [self.scrollView.mj_header beginRefreshing];
    TYBoxView *tipBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:tipBgView];
    [tipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(12);
        make.right.offset(-12);
        make.width.equalTo(scrollView.mas_width).offset(-24);
        make.height.offset(40);
    }];
    
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.font = [UIFont systemFontOfSize:17];
    tipLab.textColor = main_color;
    NSString * statusStr = @"-";
    tipLab.textColor = hexColor(CC4CD4);
    tipLab.backgroundColor = hexColorAlpha(CC4CD4,0.1);
    if([self.model.gdzt isEqualToString:@"0"]) {
        statusStr = @"待处理";
    }else if([self.model.gdzt isEqualToString:@"1"]) {
        statusStr = @"已派发";
        tipLab.textColor = hexColor(F3A642);
        tipLab.backgroundColor = hexColorAlpha(F3A642,0.1);
        
    }else if([self.model.gdzt isEqualToString:@"2"]) {
        statusStr = @"处理中";
        tipLab.textColor = hexColor(32AFFF);
        tipLab.backgroundColor = hexColorAlpha(32AFFF,0.1);
    }else if([self.model.gdzt isEqualToString:@"5"]) {
        statusStr = @"已完成";
        tipLab.textColor = hexColor(21B91D );
        tipLab.backgroundColor = hexColorAlpha(21B91D,0.1);
    }else if([self.model.gdzt isEqualToString:@"9"]) {
        statusStr = @"已关闭";
        tipLab.textColor = hexColor(606266);
        tipLab.backgroundColor = hexColorAlpha(606266,0.1);
    }
    tipLab.text = statusStr;
    tipLab.textAlignment = NSTextAlignmentCenter;
    [tipBgView addSubview:tipLab];
    self.tipLab = tipLab;
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.width.mas_greaterThanOrEqualTo(50);
        make.centerY.offset(0);
    }];
    
    UIView *topBgView = [[UIView alloc] init];
    topBgView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:topBgView];
    topBgView.layer.shadowColor = [UIColor colorWithRed:156/255.0 green:175/255.0 blue:210/255.0 alpha:0.2].CGColor;
    topBgView.layer.shadowOffset = CGSizeMake(0,4);
    topBgView.layer.shadowOpacity = 1;
    topBgView.layer.shadowRadius = 8;
    topBgView.layer.cornerRadius = 7;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"基本信息";
        [topBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        
        NSArray *arr = @[@"设备类型",@"设备编码",@"设备方向",@"设备编号",@"告警原因",@"告警时间"];
        NSArray *valueArr = @[self.model.devName?:@"", self.model.devId?:@"",self.model.direction?:@"",self.model.pileNo?:@"", self.model.cause?:@"",self.model.createTime?:@""];
        UIStackView *currentStackView = nil;
        for (int i = 0; i<arr.count; i++) {
            UIStackView *stackView1 = [[UIStackView alloc] init];
            stackView1.spacing = 10;
            [topBgView addSubview:stackView1];
            [stackView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(15);
                if(i == 0){
                    make.top.equalTo(titleLabel.mas_bottom).offset(5);
                }else {
                    make.top.equalTo(currentStackView.mas_bottom);
                }
                make.height.offset(20);
            }];
            
            UILabel *leftLab1 = [[UILabel alloc] init];
            leftLab1.text = arr[i];
            leftLab1.textColor = main_light_text_color_9;
            leftLab1.font = [UIFont systemFontOfSize:14];
            
            UILabel * ssxtLab = [[UILabel alloc] init];
            ssxtLab.text = valueArr[i];
            ssxtLab.tag = i+1000;
            ssxtLab.textColor = main_light_text_color_9;
            ssxtLab.font = [UIFont systemFontOfSize:14];
                
            [stackView1 addArrangedSubview:leftLab1];
            [stackView1 addArrangedSubview:ssxtLab];
            
            currentStackView = stackView1;
        }
        
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipBgView.mas_bottom).offset(15);
            make.left.offset(12);
            make.right.offset(-12);
            make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
            make.width.equalTo(scrollView.mas_width).offset(-24);
        }];
        
    }
    
    TYBoxView *bottomBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:bottomBgView];
    self.bottomBgView = bottomBgView;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"维修记录";
        [bottomBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        
        [bottomBgView addSubview:self.tableView];

        [bottomBgView addSubview:self.footerView];
        
        if(self.dataArr.count > 0) {
            self.footerView.hidden = YES;
        }else {
            self.footerView.hidden = NO;
            [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleLabel.mas_bottom).offset(10);
                make.left.right.offset(0);
                make.height.offset(120);
                make.bottom.offset(0);
            }];
        }
        
        
        
    }

    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView.mas_bottom).offset(15);
        make.left.right.equalTo(topBgView);
        make.bottom.equalTo(scrollView.mas_bottom);
        make.width.equalTo(scrollView.mas_width).offset(-24);
    }];
    
    [scrollView layoutIfNeeded];
    
    
    if(self.dataArr.count > 0){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView reloadData];

            [self.tableView performBatchUpdates:nil completion:^(BOOL finished) {
                CGFloat tableViewHeight = self.tableView.contentSize.height;
                // 在这里使用 tableViewHeight 更新其他 UI
                [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.offset(40);
                    make.left.right.offset(0);
                    make.height.offset(tableViewHeight);
                    make.bottom.offset(0);
                }];
                
                [scrollView layoutIfNeeded];
            }];
            
            
        });
    }
   

   
    
}
-(void)loadRequestList{
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetQueryHandleHistroy
                    parameters:@{@"devId":self.devId?:@"",@"pageNum":@(self.page),@"pageSize":@"20"}
                  successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        [weakSelf.scrollView.mj_header endRefreshing];
        [weakSelf.scrollView.mj_footer endRefreshing];
        if(success){
            if(self.page == 1){
                [weakSelf.dataArr removeAllObjects];
            }
            NSArray *arr = [NSArray yy_modelArrayWithClass:WXJLModel.class json:data[@"data"][@"list"]];
            
           [weakSelf.dataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:WXJLModel.class json:data[@"data"][@"list"]]];
            [weakSelf.tableView reloadData];
           
            [weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(self.dataArr.count *160);
            }];
            
            if(arr.count == 0 && self.page>1){
                [weakSelf.scrollView.mj_footer endRefreshingWithNoMoreData];

            }
        }
    } failureBlock:^(NSString * _Nonnull description) {
            
    }];
}
- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag;
    
    
    if(self.currentTag == tag) {
        return;
    }
    
    if(tag == 10) {// select1
        
    }else if (tag == 11){ // select2
        
    }else if (tag == 12){ // tab1
        
    }else if (tag == 13){ // tab2
        
    }
    
    self.currentTag = tag;
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GZGJDetailCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [GZGJDetailCell1 nibCell];
    }
    [cell cellWithData:self.dataArr andIndex:indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

-(UIView*)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 251)];
        UIImageView * imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gzgq_empty"]];
        
        [_footerView addSubview:imgv];
        [imgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(44);
            make.height.offset(42);
            make.centerX.offset(0);
            make.top.offset(16);
        }];
        UILabel * label = [[UILabel alloc] init];
        label.text = @"无维修记录";
        label.textColor = hexColor(C0C4CC);
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        [_footerView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(0);
            make.right.offset(0);
            make.centerX.offset(0);
            make.top.equalTo(imgv.mas_bottom).offset(10);
        }];
    }
    return _footerView;
}

@end


